
const base64 = require('./Tool/base64.js');
import PlatformUtil from "./PlatformUtil";
var httpUtils ={
 
    serverPath:"https://api.taoqi001.com/wxmp",

    //结果需要解密的请求
    httpPost: function (url,dataDic,_cbSuccess,_cbFail){

      //创建xhr对象 
         var xhr = new XMLHttpRequest();
      //设置xhr请求的超时时间
             xhr.timeout = 3000;
      // //设置响应返回的数据格式
      //        xhr.responseType ="application/json";
             var sendData = PlatformUtil.platform.params;
            // var sendData = {'appid':'com.taoqi001.wawaji','appver':117,'devid':1,'devtype': 1,'token': "e0786b7f7bf2c28c5e552a8be17a3ac3"};
             let fullData = {}

               fullData=Object.assign({},sendData,dataDic)

              // console.log("拼接的字典",fullData)
   

      //创建一个 post 请求，采用异步
      xhr.open('POST', this.serverPath+url, true);
    
      //注册相关事件回调处理函数
      xhr.onreadystatechange = function(e) { 
 
      if(xhr.readyState == 4 && xhr.status == 200){

        

           if(JSON.parse(xhr.responseText).errcode==0){

            typeof _cbSuccess == "function" && _cbSuccess(receiveDataDecode(JSON.parse(xhr.responseText).data));//如果是个函数执行后面

           }else{

             typeof _cbFail == "function" && _cbFail(JSON.parse(xhr.responseText));//如果是个函数执行后面

           }
           
       }

     };

        xhr.ontimeout = function(e) {};
        xhr.onerror = function(e) {
          
            console.log("网络请求错误")
        };
       xhr.onprogress=function(e){
        // console.log("加载进度")
       }

      //发送数据
      xhr.send(JSON.stringify(fullData));
  
},

TestPost:function(){
  console.log("测试请求")
},

//获取token的请求
getTokenPost:function(url,dataDic,_cbSuccess,_cbFail){

  //创建xhr对象 
  var xhr = new XMLHttpRequest();
  //设置xhr请求的超时时间
         xhr.timeout = 3000;
  // //设置响应返回的数据格式
  //        xhr.responseType ="application/json";
        //  var sendData = PlatformUtil.platform.params;
        var sendData = {'appid':'com.taoqi001.mp_marmot','appver':168,'devid':"6",'devtype': 2,'token': ""};
         let fullData = {}

           fullData=Object.assign({},sendData,dataDic)

  //创建一个 post 请求，采用异步
  xhr.open('POST', this.serverPath+url, true);

  //注册相关事件回调处理函数
  xhr.onreadystatechange = function(e) { 
  
  if(xhr.readyState == 4 && xhr.status == 200){

       if(JSON.parse(xhr.responseText).errcode==0){

        typeof _cbSuccess == "function" && _cbSuccess(JSON.parse(xhr.responseText).data);//如果是个函数执行后面

       }else{

         typeof _cbFail == "function" && _cbFail(JSON.parse(xhr.responseText));//如果是个函数执行后面

       }
       
   }

 };

    xhr.ontimeout = function(e) {};
    xhr.onerror = function(e) {
      
        console.log("网络请求错误")
    };
   xhr.onprogress=function(e){
    // console.log("加载进度")
   }

  //发送数据
  xhr.send(JSON.stringify(fullData));

},

NhttpPost: function (url,dataDic,_cbSuccess,_cbFail){

  //创建xhr对象 
     var xhr = new XMLHttpRequest();
  //设置xhr请求的超时时间
         xhr.timeout = 3000;
  // //设置响应返回的数据格式
  //        xhr.responseType ="application/json";
         var sendData = PlatformUtil.platform.params;
            console.log("取出来字典",sendData)
        // var sendData = {'appid':'com.taoqi001.wawaji','appver':117,'devid':1,'devtype': 1,'token': "e0786b7f7bf2c28c5e552a8be17a3ac3"};
         let fullData = {}

           fullData=Object.assign({},sendData,dataDic)

       


  //创建一个 post 请求，采用异步
  xhr.open('POST', this.serverPath+url, true);

  //注册相关事件回调处理函数
  xhr.onreadystatechange = function(e) { 
  
  if(xhr.readyState == 4 && xhr.status == 200){

      //  console.log("解密出的数据",JSON.parse(xhr.responseText))

       if(JSON.parse(xhr.responseText).errcode==0){

        typeof _cbSuccess == "function" && _cbSuccess(JSON.parse(xhr.responseText).data);//如果是个函数执行后面

       }else{

         typeof _cbFail == "function" && _cbFail(JSON.parse(xhr.responseText));//如果是个函数执行后面

       }
       
   }

 };

    xhr.ontimeout = function(e) {};
    xhr.onerror = function(e) {
      
        console.log("网络请求错误")
    };
   xhr.onprogress=function(e){
    // console.log("加载进度")
   }

  //发送数据
  xhr.send(JSON.stringify(fullData));

},


  ThttpPost: function (url,dataDic,_cbSuccess,_cbFail){

    //创建xhr对象 
    var xhr = new XMLHttpRequest();
    //设置xhr请求的超时时间
           xhr.timeout = 3000;
    // //设置响应返回的数据格式
    //        xhr.responseType ="application/json";
      console.log("交互的字典2",PlatformUtil.platform.params);
        var sendData = PlatformUtil.platform.params;
        // var sendData = {'appid':'com.taoqi001.wawaji','appver':117,'devid':1,'devtype': 1,'token': "e0786b7f7bf2c28c5e552a8be17a3ac3"};
           let fullData = {}

             fullData=Object.assign({},sendData,dataDic)


    //创建一个 post 请求，采用异步
    xhr.open('POST', this.serverPath+url, true);

    //注册相关事件回调处理函数
    xhr.onreadystatechange = function(e) { 

    if(xhr.readyState == 4 && xhr.status == 200){

         if(JSON.parse(xhr.responseText).errcode==0){
          console.log("请求正确",JSON.parse(xhr.responseText))
          typeof _cbSuccess == "function" && _cbSuccess(JSON.parse(xhr.responseText));//如果是个函数执行后面

         }else{
          console.log("请求求错误",JSON.parse(xhr.responseText))
          typeof _cbFail == "function" && _cbFail(JSON.parse(xhr.responseText));//如果是个函数执行后面
         }       
     }
    
   };

      xhr.ontimeout = function(e) {};
      xhr.onerror = function(e) {

          console.log("网络请求错误")

        };
     xhr.onprogress=function(e){
      console.log("加载进度")
     }

    //发送数据
    xhr.send(JSON.stringify(fullData));
  }

}

const base64_encode = (str) => {
    return base64.Base64.encode(str);
  }
  
  const base64_decode = (str) => {
    return base64.Base64.decode(str);
  }
  
  const alice = (str, key) => { //xor加密

    let output = '';
    let newKey = key.split("").reverse().join(""); //逆序key
    let keyLen = newKey.length - 1; //忽略key最后一个字符
    for (let i = 0; i < str.length; i++) {
      let j = i % keyLen;
      output += String.fromCharCode(str.charCodeAt(i) ^ newKey.charCodeAt(j));
    }
    
    return output
  }
  const toAlice = (str, key) => {
      
    return base64_encode(alice(str, key));
  }
  const fromAlice = (str, key) => {
    return alice(base64_decode(str), key);
  }
  
  const receiveDataDecode = (data) =>{
   
    var key 
    var decodeData

    key = toAlice(PlatformUtil.platform.params["appid"], String(PlatformUtil.platform.params["appver"]));

    // key = toAlice("com.taoqi001.wawaji", "117");

    decodeData = JSON.parse(fromAlice(data, key));
    
  

    return decodeData
   
  
  }

module.exports = httpUtils;
